<?php

class FuncDB extends AppLog
{
	var $error = "";
	var $connDB;

    function getNextValSeq($seq)
    {
        if ( $this->connDB )
        {
			$this->connDB->SetFetchMode(ADODB_FETCH_NUM);
	
			if ( ( $rs = $this->connDB->Execute( "SELECT NEXTVAL('".$seq."')" ) ) === false )
			{
				$this->logWrite("sequence $seq : ".$this->connDB->ErrorMsg(), 0);
				$this->error = "Error DB : ".$this->connDB->ErrorMsg();
				return false;
			}
	
			while ( $arr = $rs->FetchRow() ) 
			{
				return $arr[0];
			}
		}
    }
    
    function getNextValSeqAnno($data, $seq)
    {
        if ( $this->connDB )
        {
        	$tmp = split("/",$data);
        	if ( count($tmp) > 1 )
        	{
        		$anno = $tmp[2];
        	}
        	else
        	{
        		$tmp = split("-",$data);
        		if ( count($tmp) > 1 ) $anno = $tmp[0];
        	}
        	
			$this->connDB->SetFetchMode(ADODB_FETCH_NUM);
			
			$this->logWrite("sequence : SELECT $seq($anno,'next')", 9);
			
			if ( ( $rs = $this->connDB->Execute( "SELECT $seq($anno,'next')" ) ) === false )
			{
				$this->logWrite("sequence $seq : ".$this->connDB->ErrorMsg(), 0);
				$this->error = "Error DB : ".$this->connDB->ErrorMsg();
				return false;
			}
	
			while ( $arr = $rs->FetchRow() ) 
			{
				return $arr[0];
			}
		}
    }

    function getValSeqAnno($data, $cmd, $seq, $idx = 0)
    {
        if ( $this->connDB )
        {
        	$tmp = split("/",$data);
        	if ( count($tmp) > 1 )
        	{
        		$anno = $tmp[2];
        	}
        	else
        	{
        		$tmp = split("-",$data);
        		if ( count($tmp) > 1 ) $anno = $tmp[0];
        	}
        	
			$this->connDB->SetFetchMode(ADODB_FETCH_NUM);
			
			$sql = "SELECT seq_operazioni($anno,'$cmd','$seq',$idx)";
			$this->logWrite("sequence : $sql", 9);
			
			if ( ( $rs = $this->connDB->Execute( $sql ) ) === false )
			{
				$this->logWrite("sequence $seq : ".$this->connDB->ErrorMsg(), 0);
				$this->error = "Error DB : ".$this->connDB->ErrorMsg();
				return false;
			}
	
			while ( $arr = $rs->FetchRow() ) 
			{
				return $arr[0];
			}
		}
    }

    function infoFields($table, &$fields)
    {
    	$fld = $this->connDB->MetaColumns($table);
    	foreach( $fld as $key => $val)
    	{
    		$fields[ $table ][ strtolower($key) ]["type"]  		= $val->type;
    		$fields[ $table ][ strtolower($key) ]["maxLength"] 	= $val->max_length;
    	}
    }
	
	function executeSql( $sql , &$con, &$rs, &$err )
	{
		if ( ($rs = $con->Execute( $sql )) === false )
		{
			$this->logWrite("Error DB : ".$con->ErrorMsg(), 0);
			$err[] = "Error DB : ".$con->ErrorMsg();
			return true;
		}
		return false;
	}
}

?>